package com.itheima.common.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 短信发送工具类
 */
@Data
@Slf4j
@AllArgsConstructor
public class SMSUtils {

    public static final String ACCESSKEYID = "SMS_476745197"; // accessKeyId

    public static final String ACCESSKEYSECRET = "SMS_476860241";// accessKeySecret

//    public static final String SIGNNAME = "";// signName签名

    private String accessKeyId;
    private String accessKeySecret;
    private String sigName;

    /**
     * 发送短信
     *
     * @param templateCode 模板
     * @param phoneNumbers 手机号
     * @param code         参数
     */
    public void sendMessage(String templateCode, String phoneNumbers, String code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSysRegionId("cn-hangzhou");
        request.setPhoneNumbers(phoneNumbers);
        request.setSignName(sigName);
        request.setTemplateCode(templateCode);
        request.setTemplateParam("{\"code\":\"" + code + "\"}");
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(response.getMessage());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

}